package com.fand.platform.configuration.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.configuration.dto.SiteDto;
import com.fand.platform.configuration.dto.DomainNaDto;
import com.fand.platform.configuration.service.ConfigurationService;
import com.fand.platform.configuration.validator.SiteValidator;
import com.fand.platform.configuration.validator.DomainNaValidator;
import com.fand.platform.configuration.vo.SiteVo;
import com.fand.platform.configuration.service.ConfigurationService;
import com.google.common.collect.Maps;

/**
 * 站
 *
 * @author yaoHT
 */
@Controller
public class SiteController {
	@Qualifier(ConfigurationService.BEAN_ID)
	@Autowired(required = false)
	private ConfigurationService configurationService;

	@Qualifier(SiteValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator siteValidator;

	@Qualifier(DomainNaValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator domainNaValidator;

	@RequestMapping(value = { "/admin/configuration/site/site/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminSiteDelete(final SiteVo siteVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.configurationService.getSiteService().deleteSiteByIds(siteVo.getSite());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/configuration/site/site/edit.html" }, method = RequestMethod.GET)
	public String adminSiteEdit(final SiteVo siteVo, final Model model) {
		final SiteDto site = this.configurationService.getSiteService().
				getSiteById(siteVo.getSite());

		model.addAttribute("site", site);

		return "/admin/configuration/site/site/edit";
	}

	@RequestMapping(value = { "/admin/configuration/site/site.html", "/admin/configuration/site/site/index.html" })
	public String adminSiteIndex() {
		return "/admin/configuration/site/site/index";
	}

	@RequestMapping(value = { "/admin/configuration/site/site/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminSiteIndexPagination(final SiteVo siteVo) {
		final PaginationDto<Map<String, Object>> sitePagination = this.configurationService.getSiteService()
				.paginationSiteBy1ForMapList(siteVo.getSite());

		final Map<String, Object> sitePaginationMap = Maps.newHashMap();

		sitePaginationMap.put("total", sitePagination.getTotalSize());
		sitePaginationMap.put("rows", sitePagination.getDataList());

		return sitePaginationMap;
	}

	@RequestMapping(value = { "/admin/configuration/site/site/new.html" }, method = RequestMethod.GET)
	public String adminSiteNew(final SiteVo siteVo, final Model model) {
		model.addAttribute("site", siteVo.getSite());

		return "/admin/configuration/site/site/new";
	}

	@RequestMapping(value = { "/admin/configuration/site/site/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminSiteSave(final SiteVo siteVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.siteValidator.validate(siteVo.getSite(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.configurationService.getSiteService().saveSiteByIdOrCodeForSite(siteVo.getSite());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/configuration/site/site/validationBy1.json" })
	public @ResponseBody
	Boolean adminSiteValidationBy1(final SiteVo siteVo) {
		try {
			final int count = this.configurationService.getSiteService().readSiteByCodeNqIdForInteger(
					siteVo.getSite());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/configuration/site/site/view.html" }, method = RequestMethod.GET)
	public String adminSiteView(final SiteVo siteVo, final Model model) {
		final SiteDto site = this.configurationService.getSiteService().
					getSiteById(siteVo.getSite());

		model.addAttribute("site", site);

		return "/admin/configuration/site/site/view";
	}

	@RequestMapping(value = { "/admin/configuration/site/domainNa/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminDomainNaDelete(final SiteVo siteVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.configurationService.getSiteService().deleteDomainNaByIds(siteVo.getDomainNa());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/configuration/site/domainNa/edit.html" }, method = RequestMethod.GET)
	public String adminDomainNaEdit(final SiteVo siteVo, final Model model) {
		final DomainNaDto domainNa = this.configurationService.getSiteService().
				getDomainNaById(siteVo.getDomainNa());

		model.addAttribute("domainNa", domainNa);

		return "/admin/configuration/site/domainNa/edit";
	}

	@RequestMapping(value = { "/admin/configuration/site/domainNa.html", "/admin/configuration/site/domainNa/index.html" })
	public String adminDomainNaIndex() {
		return "/admin/configuration/site/domainNa/index";
	}

	@RequestMapping(value = { "/admin/configuration/site/domainNa/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminDomainNaIndexPagination(final SiteVo siteVo) {
		final PaginationDto<Map<String, Object>> domainNaPagination = this.configurationService.getSiteService()
				.paginationDomainNaBy1ForMapList(siteVo.getDomainNa());

		final Map<String, Object> domainNaPaginationMap = Maps.newHashMap();

		domainNaPaginationMap.put("total", domainNaPagination.getTotalSize());
		domainNaPaginationMap.put("rows", domainNaPagination.getDataList());

		return domainNaPaginationMap;
	}

	@RequestMapping(value = { "/admin/configuration/site/domainNa/new.html" }, method = RequestMethod.GET)
	public String adminDomainNaNew(final SiteVo siteVo, final Model model) {
		model.addAttribute("domainNa", siteVo.getDomainNa());

		return "/admin/configuration/site/domainNa/new";
	}

	@RequestMapping(value = { "/admin/configuration/site/domainNa/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminDomainNaSave(final SiteVo siteVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.domainNaValidator.validate(siteVo.getDomainNa(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.configurationService.getSiteService().saveDomainNaByIdOrCodeForDomainNa(siteVo.getDomainNa());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/configuration/site/domainNa/validationBy1.json" })
	public @ResponseBody
	Boolean adminDomainNaValidationBy1(final SiteVo siteVo) {
		try {
			final int count = this.configurationService.getSiteService().readDomainNaByCodeNqIdForInteger(
					siteVo.getDomainNa());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/configuration/site/domainNa/view.html" }, method = RequestMethod.GET)
	public String adminDomainNaView(final SiteVo siteVo, final Model model) {
		final DomainNaDto domainNa = this.configurationService.getSiteService().
					getDomainNaById(siteVo.getDomainNa());

		model.addAttribute("domainNa", domainNa);

		return "/admin/configuration/site/domainNa/view";
	}
}
